<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // 求 1- 100 的和

      function getSum(num) {
        if (num === 1) {
          return 1
        }
        return getSum(num - 1) + num
      }
      //   console.log(getSum(100))

      //    递归实现
      //    1  2  3  4  第几个月
      //    1  1  2  3
      //   function getNum(num) {
      //     if (num === 1 || num === 2) {
      //       return 1
      //     }
      //     return getNum(num - 1) + getNum(num - 2)
      //   }
      //   console.log(getNum(4)) // 求第三个月
      const cache = []
      function getNum(num) {
        if (num === 1 || num === 2) {
          return 1
        }
        if (!cache[num]) {
          cache[num] = getNum(num - 1) + getNum(num - 2)
        }
        return cache[num]
      }
      console.log(getNum(48)) // 求第三个月
    </script>
  </body>
</html>
